package com.mzy.javaapi.Map;


import com.mzy.common.Student;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Ives
 * * @date 2021/6/21
 */
public class MapIteration1 {
  public static void main(String[] args) {
    HashMap<Integer, Student> studentHashMap = new HashMap<>();
    for (int i = 0; i < 10; i++) {
      Student student = new Student();
      student.setName("Lisi"+i);
      student.setAge(i);
      studentHashMap.put(i,student);
    }

    // 打印map中的key
    for (Integer key:studentHashMap.keySet()
         ) {
      System.out.print(" "+key);
    }
    System.out.println();
    // 打印map中的value
    for (Student value:studentHashMap.values()
         ) {
      System.out.println(value);
    }
    // 使用entrySet()遍历
    for (Map.Entry<Integer, Student> entry:
         studentHashMap.entrySet()
         ) {
      System.out.print("entrySet遍历"+entry.getKey());
      System.out.println("value:"+entry.getValue());

    }
    // 迭代器遍历
    Iterator<Integer> iterator = studentHashMap.keySet().iterator();
    while (iterator.hasNext()){

      Integer key = iterator.next();
      System.out.println("key: "+key+ "value: "+studentHashMap.get(key));
    }
    // 迭代器+entrySet
    Iterator<Map.Entry<Integer, Student>> iterator1 = studentHashMap.entrySet().iterator();
    while (iterator1.hasNext()){
      Map.Entry<Integer, Student> key = iterator1.next();
      System.out.println("迭代器+entrySet,key: "+key.getKey()+ "value: "+key.getValue());
    }
  }
}
